from microbit import *
import music

mv = 255


def get_beat_time(b):
    v = max(0, b)
    t = music.get_tempo()[0]
    return int(60000 * v / t)


def sleep_beat(b):
    sleep(get_beat_time(b))


def change_music_tempo(c, bpm):
    t = music.get_tempo()[0]
    v = bpm
    if c == 'increase':
        v = t + v
    else:
        v = t - v
    music.set_tempo(bpm=v)


def set_music_volume(v):
    r = max(0, min(255, v))
    set_volume(r)
    global mv
    mv = r


def get_music_volume():
    global mv
    return mv
